using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_Pressure : Form
    {
        public Sec_Scan_Pressure()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("并发地址不能为空！");
                return;
            }
            else
            {
                string url = textBox1.Text;
                webBrowser1.Navigate(url);
            }
        }
        public static AutoResetEvent myEvent = new AutoResetEvent(false);
        //public const int cycleNum = ;//循环量
        public static int cnt = 10;
        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox6.Text.Length > 7)
            {
                //MessageBox.Show("循环量大于千万情况下，可能会导致死机！是否继续！");
                if (MessageBox.Show("循环量大于千万数量级别的情况下，可能会导致设备死机！是否继续！", "操作警告！", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                {
                    int cycleNum = Convert.ToInt16(textBox6.Text);//循环量
                    //执行独立并发 Independent concurrency
                    if (string.IsNullOrEmpty(textBox1.Text))
                    {
                        MessageBox.Show("并发地址不能为空！");
                    }
                    else
                    {
                        Task.Run(() =>
                        {
                            ThreadPool.SetMinThreads(200, 200);
                            ThreadPool.SetMaxThreads(200, 200);
                            for (int i = 1; i <= cycleNum; i++)
                            {
                                ThreadPool.QueueUserWorkItem(new WaitCallback(TestFun), i.ToString());
                            }
                            richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + "主线程执行中" + "\r\n";
                            richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + "主线程已结束" + "\r\n";
                            // 阻止当前线程，直到当前 WaitHandle 收到信号。
                            myEvent.WaitOne();
                            richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + "线程池终止" + "\r\n";
                            Console.ReadKey();
                        });

                    }
                }
                else
                {
                    //MessageBox.Show("否");
                }
            }
            else
            {
                int cycleNum = Convert.ToInt32(textBox6.Text);//循环量
                //执行独立并发 Independent concurrency
                if (string.IsNullOrEmpty(textBox1.Text))
                {
                    MessageBox.Show("并发地址不能为空！");
                }
                else
                {
                    Task.Run(() =>
                    {
                        ThreadPool.SetMinThreads(200, 200);
                        ThreadPool.SetMaxThreads(200, 200);
                        for (int i = 1; i <= cycleNum; i++)
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(TestFun), i.ToString());
                        }
                        richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + "主线程执行中" + "\r\n";
                        richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + "主线程已结束" + "\r\n";
                        // 阻止当前线程，直到当前 WaitHandle 收到信号。
                        myEvent.WaitOne();
                        richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + "线程池终止" + "\r\n";
                        Console.ReadKey();
                    });

                }
            }   
        }
        public void TestFun(object obj)
        {
            Sec_Api.Sec_IndepenDent_Concurrency_Service_Api.Url = textBox1.Text;
            Sec_Service.Sec_IndepenDent_Concurrency_Service.IndepenDent_Service(Sec_Api.Sec_IndepenDent_Concurrency_Service_Api.I1);//执行并发
            string open = Sec_Api.Sec_IndepenDent_Concurrency_Service_Api.StatusOpen.ToString();
            //MessageBox.Show(open);
            richTextBox1.Text = open + "\r\n";
            richTextBox1.Text += "执行时间：" + DateTime.Now + "   ------   " + string.Format("{0}:第{1}个线程,当前线程为{2}", DateTime.Now.ToString(), obj.ToString(),
            Thread.CurrentThread.ManagedThreadId.ToString()) + "\r\n";
            //Console.WriteLine(string.Format("{0}:第{1}个线程,当前线程为{2}", DateTime.Now.ToString(), obj.ToString(),
            //Thread.CurrentThread.ManagedThreadId.ToString()));//获取线程池
            // 这种方法可避免信号的终止
            cnt -= 1;
            if (cnt == 0)
            {
                myEvent.Set();
            }
        }
    }
}
